package com.connector.server.util;

import org.slf4j.LoggerFactory;
import com.connector.base.connection.impl.SocketIoConnection;
import com.connector.base.connection.impl.NettyConnection;
import com.google.common.collect.Lists;
import com.alibaba.fastjson.JSON;
import com.connector.base.message.model.PushMessage;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import com.connector.base.connection.Connection;
import com.connector.base.connection.channel.SocketChannel;
import java.util.concurrent.ConcurrentMap;
import com.connector.base.protocol.Packet;
import java.util.List;
import org.slf4j.Logger;

public class PushUtil
{
    private static final Logger log;
    private static final String JEPLUS_ALL_USER_ID = "JEPLUS_ALL_USER_ID";

    public static void pushMsg(final List<String> userIds, final String msgId, final String msgType, final Packet packet, final ConcurrentMap<SocketChannel, Connection> allConnection, final String message, String must, final String onlyMobile) {
        if (StringUtils.isEmpty((CharSequence)must)) {
            must = "0";
        }
        if (null != userIds && !userIds.isEmpty()) {
            final String firstUserId = userIds.get(0);
            final Set<Connection> connections =  Sets.newHashSet();
            final Set<String> webUserIds = Sets.newHashSet();
            if (firstUserId.equals("JEPLUS_ALL_USER_ID")) {
                for (final Connection connection : allConnection.values()) {
                    connections.add(connection);
                }
                sendToConnection(connections, packet, message);
            }
            else {
                if (StringUtils.isNotEmpty((CharSequence)onlyMobile) && "1".equals(onlyMobile)) {
                    must = "0";
                    for (final String userId : userIds) {
                        for (final Connection c : allConnection.values()) {
                            final String uId = c.getSessionContext().getUserId();
                            if (StringUtils.isNotBlank((CharSequence)uId) && StringUtils.isNotBlank((CharSequence)userId) && userId.equals(uId)) {
                                final String mobile = c.getSessionContext().getMobile();
                                if (!StringUtils.isNotEmpty((CharSequence)mobile) || !mobile.equals("2")) {
                                    continue;
                                }
                                connections.add(c);
                            }
                        }
                    }
                }
                else {
                    for (final String userId : userIds) {
                        for (final Connection c : allConnection.values()) {
                            final String uId = c.getSessionContext().getUserId();
                            if (StringUtils.isNotBlank((CharSequence)uId) && StringUtils.isNotBlank((CharSequence)userId) && userId.equals(uId)) {
                                connections.add(c);
                                webUserIds.add(userId);
                            }
                        }
                    }
                }
                sendToConnection(connections, packet, message);
                if (must.equals("1")) {
                    final Map<String, String> paramMap = Maps.newHashMap();
                    try {
                        paramMap.put("allUser", StringUtils.join((Iterable)userIds, ","));
                        if (webUserIds.isEmpty()) {
                            paramMap.put("onlionUser", "");
                        }
                        else {
                            paramMap.put("onlionUser", StringUtils.join((Iterable)webUserIds, ","));
                        }
                        paramMap.put("msgId", msgId);
                        paramMap.put("msgType", msgType);
                        HttpUtil.postToInstantServer_updateMsgStatus(paramMap);
                    }
                    catch (Exception e) {
                        PushUtil.log.warn("更新消息状态出现错误{}", (Object)e.getMessage());
                    }
                }
            }
        }
    }

    public static void pushNoReadMsg(final String userId, final String text, final ConcurrentMap<SocketChannel, Connection> allConnection) {
        final PushMessage pushMessage = new PushMessage();
        pushMessage.setData(text);
        pushMessage.setType("5");
        final String message = JSON.toJSONString(pushMessage);
        final Set<Connection> connections = Sets.newHashSet();
        for (final Connection c : allConnection.values()) {
            final String uId = c.getSessionContext().getUserId();
            if (Lists.newArrayList((Object[])new String[] { userId }).contains(uId)) {
                connections.add(c);
            }
        }
        sendToConnection(connections, PushMessage.buildPacket(message), message);
    }

    public static void sendCertMsg(final Connection connection, final String obj) {
        final PushMessage pushMessage = new PushMessage();
        pushMessage.setData(obj);
        pushMessage.setType("4");
        final String message = JSON.toJSONString(pushMessage);
        final Set<Connection> connections =  Sets.newHashSet();
        connections.add(connection);
        sendToConnection(connections, PushMessage.buildPacket(message), message);
    }

    public static void sendToConnection(final Set<Connection> connections, final Packet packet, final String message) {
        if (null != connections) {
            if (!connections.isEmpty()) {
                for (final Connection connection : connections) {
                    if (connection instanceof NettyConnection) {
                        connection.channelSend((Object)packet);
                    }
                    if (connection instanceof SocketIoConnection) {
                        connection.sendEvent("message", new Object[] { message });
                    }
                }
            }
        }
    }

    public static void sendOtherMsg(final String userId, final String msg, final ConcurrentMap<SocketChannel, Connection> allConnection) {
        final PushMessage pushMessage = new PushMessage();
        pushMessage.setData(msg);
        pushMessage.setType("4");
        final String message = JSON.toJSONString( pushMessage);
        final Set<Connection> connections = Sets.newHashSet();
        if (StringUtils.isEmpty(userId)) {
            for (final Connection c : allConnection.values()) {
                final String uId = c.getSessionContext().getUserId();
                if (StringUtils.isNotEmpty(uId)) {
                    connections.add(c);
                }
            }
        }
        else {
            for (final Connection c : allConnection.values()) {
                final String uId = c.getSessionContext().getUserId();
                if (Lists.newArrayList((Object[])new String[] { userId }).contains(uId)) {
                    connections.add(c);
                }
            }
        }
        sendToConnection(connections, PushMessage.buildPacket(message), message);
    }

    static {
        log = LoggerFactory.getLogger((Class)PushUtil.class);
    }
}
